#include <stdlib.h>
#include <stdio.h>
#include <SDL.h>



void menu(SDL_Surface *ecran);

int main(int argc, char *argv[])
{
    SDL_Surface *ecran;
    SDL_Init(SDL_INIT_VIDEO);
    ecran=SDL_SetVideoMode(600, 500, 32, SDL_HWSURFACE );

    SDL_WM_SetCaption("cryptage, decryptage 2P2", NULL);
     SDL_WM_SetIcon(SDL_LoadBMP("images.bmp"), NULL);
    SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 0, 0, 255));
    SDL_Flip(ecran);
    menu(ecran);


    SDL_Quit();

    return EXIT_SUCCESS;
    SDL_FreeSurface(ecran);
}

void menu(SDL_Surface *ecran)

{
    SDL_Rect position,position_menu,posp;
    SDL_Event event;
    int m,a,aa,s,n,j,d,o;
    int continuer = 1;
    SDL_Surface* menu1_bmp = SDL_LoadBMP("m1.bmp");
    SDL_Surface* menu2_bmp = SDL_LoadBMP("m2.bmp");
    SDL_Surface* menu3_bmp = SDL_LoadBMP("m3.bmp");
    SDL_Surface* aide_bmp = SDL_LoadBMP("p.bmp");
    SDL_Surface* niveau1_bmp = SDL_LoadBMP("niveau1.bmp");
    SDL_Surface* niveau2_bmp = SDL_LoadBMP("niveau2.bmp");
    SDL_Surface* jeu1_bmp = SDL_LoadBMP("jeu1.bmp");

    position_menu.x =0;
    position_menu.y =0;

    SDL_BlitSurface(menu1_bmp, NULL, ecran, &position_menu);
    SDL_Flip(ecran);


    m=0;
    n=0;
    a=0;
    j=0;
    while (continuer)
    {
        while(SDL_PollEvent(&event))
        {
            switch (event.type)
            {
            case SDL_QUIT:
                continuer = 0;
            case SDL_KEYDOWN:
            {
                if (event.key.keysym.sym == SDLK_ESCAPE && m==0 && a==0 &&n==0)
                    continuer=0;
                if (event.key.keysym.sym == SDLK_ESCAPE && ( a!=0  || n!=0 || j!=0 ))
                {

                    a=0;
                    n=0;
                    j=0;


                }
                if (event.key.keysym.sym == SDLK_BACKSPACE)
                    continuer=0;
                if (event.key.keysym.sym == SDLK_RETURN && m==2)
                    continuer=0;

                if (event.key.keysym.sym == SDLK_RETURN && m==1)

                    a++;

                if (event.key.keysym.sym == SDLK_DOWN && a==0 && o==0  && n==0)
                    m++;
                if (event.key.keysym.sym == SDLK_UP && a==0 && o==0  && n==0)
                    m--;
                    if (event.key.keysym.sym == SDLK_RETURN && m==0 && n==0)
                        n++;
                    if (event.key.keysym.sym == SDLK_DOWN && m==0 && n!=0&&j==0)
                        {if(n==2)
                            n=n+2;
                       else
                        n++;}
                    if (event.key.keysym.sym == SDLK_UP && m==0 && n!=0&&j==0)
                        if(n==1)
                        n=n+2;
                        else n--;
                    if (event.key.keysym.sym == SDLK_p && m==0 && n==1 && j==0)
                        j++;



break;
            }

            }




        }

          //partie 2 : affichage des entites

        SDL_BlitSurface(menu1_bmp, NULL, ecran, &position_menu);
        m=m%5;
        n=n%3;

        if(m == 0 )
            {SDL_BlitSurface(menu1_bmp, NULL, ecran, &position_menu);

            if(n==1)
             {SDL_BlitSurface(niveau1_bmp, NULL, ecran, &position_menu);

        }
            if(n==2)
                SDL_BlitSurface(niveau2_bmp, NULL, ecran, &position_menu);
            }
        if(m == 1)
            {
                SDL_BlitSurface(menu2_bmp, NULL, ecran, &position_menu);
            }



        if(m == 2)
        {
            SDL_BlitSurface(menu3_bmp, NULL, ecran, &position_menu);
            if(a==1)

              SDL_BlitSurface(aide_bmp, NULL, ecran, &position_menu);
            }
            //if(m==0 && n==1&& j==1)
        //SDL_BlitSurface(jeu1_bmp, NULL, ecran, &position_menu);





        SDL_Flip(ecran); //action  3 et 4 dans game loop

            }
}
